package com.etocrm.saas.lib.base.common.result;

import com.etocrm.saas.lib.base.common.exception.BaseException;
import com.etocrm.saas.lib.base.common.obj.BaseO;
import lombok.Getter;
import lombok.Setter;

/**
 * 统一的返回结果对象
 */
@Getter
@Setter
public class Result<T> extends BaseO implements IResult<T> {

    private static final long serialVersionUID = 1L;

    private Integer code;
    private String message;
    private T data;

//    private String requestId;

    public Result(IResultCode status, T data) {
        this.code = status.getCode();
        this.message = status.getMessage();
        this.data = data;
    }

    public Result(IResultCode status) {
        this.code = status.getCode();
        this.message = status.getMessage();
    }

    public Result(BaseException e) {
        this.code = e.getCode();
        this.message = e.getLocalizedMessage();
    }

    public Result(BaseException e, T data) {
        this.code = e.getCode();
        this.message = e.getLocalizedMessage();
        this.data = data;
    }

    public Result(IResultCode status, String msg, T data) {
        this.code = status.getCode();
        this.message = msg;
        this.data = data;
    }

    public Result() {
    }

    public Result(Result<T> result) {
        this.code = result.getResultCode();
        this.message = result.getMessage();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return new Result<>(ResultCodeEnum.SUCCESS, null);
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResultCodeEnum.SYSTEM_ERROR, null);
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = new Result<>(ResultCodeEnum.SYSTEM_ERROR, null);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fail(Integer code,String msg) {
        Result<T> result = new Result<>(ResultCodeEnum.SYSTEM_ERROR, null);
        result.setMessage(msg);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> fail(IResultCode status) {
        return new Result<>(status);
    }

    @Override
    public Integer getResultCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
